//
//  LRXFixNameView.m
//  路人侠
//
//  Created by zhangxulong on 4/11/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXFixNameView.h"
@interface LRXFixNameView()
@property (nonatomic,strong) UITextField * NameText;
@end



@implementation LRXFixNameView

+ (instancetype)fixNameView:(NSString * )oldName
{
    return [[LRXFixNameView alloc]initFixNameView:oldName];
}
- (instancetype)initFixNameView:(NSString * )oldName
{
    if (self = [super init]) {
        
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 5 * hScale;
        self.layer.masksToBounds = YES;
        self.frame = CGRectMake(42 * wScale, 400 * hScale, 554 * wScale, 320 * hScale);
        
        UILabel * headLabel = [[UILabel alloc]init];
        headLabel.text = @"修改用户名";
        headLabel.textColor = mainColor;
        headLabel.font = [UIFont systemFontOfSize:30 * hScale];
        headLabel.frame = CGRectMake(35 * wScale, 39 * hScale, 0, 0);
        [headLabel sizeToFit];
        [self addSubview:headLabel];
        
      
        
        
        UITextField * NameText = [[UITextField alloc]init];
        NameText.text = oldName;
        NameText.font = [UIFont systemFontOfSize:30 * hScale];
        NameText.frame = CGRectMake(30 * wScale, CGRectGetMaxY(headLabel.frame) + 40 * hScale, 444 * wScale, 40 * hScale);
        NameText.textAlignment = NSTextAlignmentNatural;
        self.NameText = NameText;
        
        [self addSubview:NameText];
        UIView * lineView = [[UIView alloc]init];
        lineView.frame = CGRectMake(30 * wScale, CGRectGetMaxY(NameText.frame) + 10 * hScale, self.frame.size.width -  60 * wScale, 2 * hScale);
        lineView.backgroundColor = mainColor;
        [self addSubview:lineView];
        
        
        UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        cancelBtn.frame = CGRectMake(247 * wScale, 240 * hScale, 68 * wScale, 32 * hScale);
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:34 * hScale];
        [cancelBtn addTarget:self action:@selector(cancelUpdateUserName) forControlEvents:UIControlEventTouchUpInside];
        [cancelBtn sizeToFit];
        
        UIButton * accpBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [accpBtn setTitle:@"确定" forState:UIControlStateNormal];
        accpBtn.frame = CGRectMake(416 * wScale, 240 * hScale, 68 * wScale, 32 * hScale);
        accpBtn.titleLabel.font = [UIFont systemFontOfSize:34 * hScale];
        [accpBtn addTarget:self action:@selector(updateUserName) forControlEvents:UIControlEventTouchUpInside];
        [accpBtn sizeToFit];
        
        [self addSubview:cancelBtn];
        [self addSubview:accpBtn];
    }
    return self;
}


- (void)updateUserName
{
    if([self.fixNameViewDelegate respondsToSelector:@selector(updateUserName:)])
    {
        [self.fixNameViewDelegate updateUserName:self.NameText.text];
    }
}
- (void)cancelUpdateUserName
{
    if ([self.fixNameViewDelegate respondsToSelector:@selector(cancelUserName)]) {
        [self.fixNameViewDelegate cancelUserName];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

@end
